package cn.gyyx.tools;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gyyx.tools.sign.beans.CallResult;
import cn.gyyx.tools.sign.beans.ExtResult;
import cn.gyyx.tools.sign.service.CallService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gyyx.tools.sign.service.SignService;

@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);
        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
        String formattedDate = dateFormat.format(date);
        model.addAttribute("serverTime", formattedDate);
        return "home";
    }

    /**
     * 对传入的Url进行加密
     *
     * @param locale
     * @param model
     * @return
     */
    @RequestMapping(value = "sign/signurl", method = RequestMethod.POST)
    public
    @ResponseBody
    ExtResult<SignResultView> signUrl(HttpServletRequest request,
                                      HttpServletResponse response, Locale locale, Model model) {

        ExtResult<SignResultView> extResult = new ExtResult<SignResultView>();
        SignResultView view = new SignResultView();
        extResult.setData(view);

        //获取Unix时间戳
        long timestamp = System.currentTimeMillis() / 1000;

        String originUrl = request.getParameter("originUrl");
        String signKey = request.getParameter("signKey");
        SignService service = new SignService();
        ExtResult<String> result = service.signUrl(originUrl, signKey,timestamp);

        view.setSignUrl(result.getData());
        view.setTimeStamp(String.valueOf(timestamp));

        CallService callService=new CallService();
        ExtResult<CallResult> callExtResult = callService.call(view.getSignUrl());

        view.setHttpStatusCode(callExtResult.getData().getHttpStatusCode());
        view.setBody(callExtResult.getData().getBody());

        return extResult;
    }

}

class SignResultView {

    /**
     * 时间戳
     */
    private String timeStamp;

    /**
     * 请求地址
     */
    private String signUrl;

    /**
     * Http状态码
     */
    private int httpStatusCode;

    /**
     * 请求的返回内容
     */
    private String body;

    public String getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(String timeStamp) {
        this.timeStamp = timeStamp;
    }

    public String getSignUrl() {
        return signUrl;
    }

    public void setSignUrl(String signUrl) {
        this.signUrl = signUrl;
    }

    public int getHttpStatusCode() {
        return httpStatusCode;
    }

    public void setHttpStatusCode(int httpStatusCode) {
        this.httpStatusCode = httpStatusCode;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }
}
